package com.frxuexi.park2304.notice

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.http.WebScoketClient
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityNoticeBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NoticeActivity")
@AndroidEntryPoint
class NoticeActivity : BaseActivity<ActivityNoticeBinding,NoticeIntent,NoticeViewModel>() {
    override val binding: ActivityNoticeBinding by lazy { ActivityNoticeBinding.inflate(layoutInflater) }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()

    var id=-1
    @RequiresApi(Build.VERSION_CODES.O)
    override fun initData() {
        WebScoketClient.myDate.observe(this){
            if(!it.contains("|")) return@observe
            val split = it.split("|")
            val manager = getSystemService(NotificationManager::class.java)
            val channel =
                NotificationChannel("noti", "发布公告", NotificationManager.IMPORTANCE_LOW)
            manager.createNotificationChannel(channel)
            val intent = Intent(this, NoticeDetailActivity::class.java)
            intent.putExtra("id",id)
            val pendingIntent = PendingIntent.getActivity(
                this, 101, intent,
                PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
            )
            val notification = Notification.Builder(this, "noti")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle(split[0])
                .setContentText(split[1])
                .setContentIntent(pendingIntent)
                .build()
            manager.notify(1,notification)
        }

        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val item = it.data as NoticeBeanItem
                        id=item.id!!
                        WebScoketClient.sendMessage("${item.title}|${item.content}")
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        WebScoketClient.start()
        binding.noticeCommit.setOnClickListener {
            val content = binding.noticeContent.text.toString()
            val title = binding.noticeTitle.text.toString()
            val type = binding.noticeType.text.toString()

            val item = NoticeBeanItem(
                content,
                null,
                DateTimeUtils.getDateTime(System.currentTimeMillis()),
                "发布",
                title,
                type,
                SPUtils.getInstance().getInt(Const.UID),0
            )
            vm.sendIntent(NoticeIntent.addNotice(item))
        }
    }

}